package logic

import (
	"context"
	"rk155/pkg"
	"rk155/rk155rpc/rk155rpc"
	"strconv"

	"rk155/api/internal/svc"
	"rk155/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RegisterLogic) Register(req *types.RegisterRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	register, err := l.svcCtx.Rk155Rpc.Register(l.ctx, &rk155rpc.RegisterRequest{
		Tel:        req.Tel,
		Password:   req.Password,
		WxChatName: req.WxChatName,
		HeadImg:    req.HeadImg,
		UserId:     int64(req.UserId),
	})
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    "500",
			Data:    "",
		}, nil
	}

	handler, err := pkg.TokenHandler(strconv.FormatInt(register.Id, 10))
	if err != nil {
		return &types.Response{
			Message: err.Error(),
			Code:    "500",
			Data:    "",
		}, nil
	}

	return &types.Response{
		Message: "注册成功",
		Code:    "200",
		Data:    handler,
	}, nil
}
